package com.daxt.controller.fileuse;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.daxt.mapper.sys.UserMapper;
import com.daxt.model.service.base.vo.BaseUser;
import com.daxt.service.UseBorrowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.daxt.common.result.PageInfo;
import com.daxt.common.result.PageUtil;
import com.daxt.common.result.Result;
import com.daxt.common.result.ResultCode;
import com.daxt.common.result.ResultUtil;
import com.daxt.mapper.use.UseBorrowRecordMapper;
import com.daxt.model.service.use.vo.UseBorrowRecord;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@Api(tags = "档案利用")
@RestController
@RequestMapping("/fileUseManager")
public class BorrowController {
	
	@Autowired
	private UseBorrowRecordMapper useBorrowRecordMapper;
	@Autowired
	private UseBorrowService useBorrowService;
	@Autowired
	private UserMapper userMapper;

	//@Autowired
	//private RedisUtil redisUtil;


	
	@ApiOperation(value = "借阅列表", notes = "") 
	@GetMapping("/borrow") 
	public Result<PageInfo<UseBorrowRecord>> identList(@ApiParam("") @RequestParam(value = "pageNum", required = false)Integer pageNum,
			@ApiParam("") @RequestParam(value = "pageSize", required = false)Integer pageSize,
			@ApiParam("") @RequestParam(value = "title", required = false)String title,
			@ApiParam("") @RequestParam(value = "startTime", required = false)String startTime,
			@ApiParam("") @RequestParam(value = "endTime", required = false)String endTime,
			@ApiParam("") @RequestParam(value = "classId", required = false)String classId,
			@ApiParam("") @RequestParam(value = "typeId", required = false)String typeId,
			@ApiParam("借阅状态（0待审批、1已拒绝、2借阅中、3已归还") @RequestParam(value = "state", required = false)String state,
			@ApiParam("") @RequestParam(value = "fondsId", required = false)String fondsId,
			@ApiParam("") @RequestParam(value = "username", required = false)String username,
			@ApiParam("") @RequestParam(value = "role", required = false)String role){

		if("admin".equals(username) || "超级管理员".equals(role)){
			fondsId=null;
		}

		Page<UseBorrowRecord> page=new Page<>(pageNum==null?0:pageNum, pageSize==null?10:pageSize);
		QueryWrapper<UseBorrowRecord> queryWrapper=new QueryWrapper<>();
		if(!StringUtils.isEmpty(title)) {
		queryWrapper.eq("title", title);
		}
		if(!StringUtils.isEmpty(classId)) {
			queryWrapper.eq("classId", classId);
		}
		if(!StringUtils.isEmpty(typeId)) {
			queryWrapper.eq("typeId", typeId);
		}
		if(!StringUtils.isEmpty(fondsId)) {
			queryWrapper.eq("fondsId", fondsId);
		}
		if(!StringUtils.isEmpty(state)) {
			queryWrapper.eq("state", state);
		}
		/*if(!StringUtils.isEmpty(startTime)&&!StringUtils.isEmpty(endTime)) {
			queryWrapper.ge("startTime", startTime);
			queryWrapper.le("endTime", endTime);
		}*/
		if(!StringUtils.isEmpty(startTime)) {
			queryWrapper.ge("startTime", startTime);
		}
		if(!StringUtils.isEmpty(endTime)) {
			queryWrapper.le("endTime", endTime);
		}

		queryWrapper.orderByDesc("createTime");
		IPage<UseBorrowRecord> info = useBorrowRecordMapper.selectPage(page, queryWrapper);
		useBorrowService.buildArchives(info.getRecords());
		for(int i=0;i<info.getRecords().size();i++){
			String  createBy=info.getRecords().get(i).getCreateBy();
			if(!StringUtils.isEmpty(createBy)){
				BaseUser baseUser= userMapper.selectOne(new QueryWrapper<BaseUser>().eq("username",createBy));
				if(baseUser!=null) info.getRecords().get(i).setCreateBy(baseUser.getRealName());
			}
		}
		PageInfo<UseBorrowRecord> data = PageUtil.data(info.getPages(), info.getCurrent(), info.getTotal(), info.getRecords());
		return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(),"", data);
	}
	
	@ApiOperation(value = "借阅修改", notes = "") 
	@PostMapping("/borrow") 
	public Result<String> edit(@RequestBody UseBorrowRecord useBorrowRecord,HttpServletRequest request){
		//UserInfo loginInfo = UserInfoUtil.getLoginInfo(UserInfo.class, redisUtil, request);
		//useBorrowRecord.setCreateTime(LocalDateTimeUtil.getNowTimeYMD());
		//useBorrowRecord.setUserId(loginInfo.getId());
		useBorrowRecordMapper.updateById(useBorrowRecord);
		return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(),"",null);
	}


	@ApiOperation(value = "借阅归还", notes = "")
	@PostMapping("/borrow/giveBack/{id}")
	public Result<String> giveBack(@PathVariable("id") String id){

		UseBorrowRecord borrow = useBorrowRecordMapper.selectById(id);
		borrow.setState("3");
		useBorrowRecordMapper.updateById(borrow);
		return ResultUtil.success();
	}

}
